package cn.tedu.controller;

import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Arrays;

@RestController
@RequestMapping("student")
public class StudentController {
    @RequestMapping("sava")
    public String save(Student s) throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接(url user pwd   )
        String url = "jdbc:mysql://localhost:3306/cgb210801?characterEncoding=utf8";
        String user = "root";
        String pwd = "root";
        Connection c = DriverManager.getConnection(url,user,pwd);
        //3.获取传输器(用新传输器，否则sql攻击)
        String sql ="insert into tb_student values (null,?,?,?,?,?,?)";
        PreparedStatement ps = c.prepareStatement(sql);
        //TODD 给SQL设置参数
        ps.setString(1,s.getUser());
        ps.setInt(2,s.getAge());
        ps.setInt(3,s.getSex());
        ps.setObject(4,s.getHobby());
        ps.setObject(5,s.getEdu());
        ps.setObject(6,s.getIntime());

        //4.执行sql(insert)
        ps.executeUpdate();
        //5.关闭资源
        ps.close();
        c.close();
        System.out.println("数据入库成功!");
        return "访问成功~"+s;
    }



}
